﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Threading;
using SoufunData.DB;

namespace SoufunData
{
    public partial class BProjectListForm : DevExpress.XtraEditors.XtraForm
    {
        public BProjectListForm()
        {
            InitializeComponent();
        }

        private bool _isDeletingProject;

        private void BProjectListForm_Load(object sender, EventArgs e)
        {
            textEdit_OpenTimeBegin.Text = "2000年1月";
            textEdit_OpenTimeEnd.Text = DateTime.Now.ToString("yyyy年M月");
            Thread t = new Thread(LoatData) { IsBackground = true };
            t.Start();
        }

        private void LoatData()
        {
            StringBuilder strWhere = new StringBuilder();
            strWhere.Append("t.OpenTime between '");
            strWhere.Append(DateTime.Parse(textEdit_OpenTimeBegin.Text));
            strWhere.Append("' and '");
            strWhere.Append(DateTime.Parse(textEdit_OpenTimeEnd.Text));
            strWhere.Append("' ");
            if (textEdit_proName.Text.Trim()!="")
            {
                strWhere.Append(" and  t.ProjectName like '%");
                strWhere.Append(textEdit_proName.Text);
                strWhere.Append("%' ");
            }
            if (textEdit_BuildBusiness.Text.Trim() != "")
            {
                strWhere.Append(" and  t.BuildBusiness like '%");
                strWhere.Append(textEdit_BuildBusiness.Text);
                strWhere.Append("%' ");
            }
            if (textEdit_PropertyCompany.Text.Trim() != "")
            {
                strWhere.Append(" and  t.PropertyCompany like '%");
                strWhere.Append(textEdit_PropertyCompany.Text);
                strWhere.Append("%' ");
            } 
            if (imgCombo_SellState.SelectedIndex != 0)
            {
                strWhere.Append(" and t.SellState ='");
                strWhere.Append(imgCombo_SellState.Properties.Items[
                    imgCombo_SellState.SelectedIndex].Description);
                strWhere.Append("' ");
            }

            if (imgCombox_BusinessType.SelectedIndex!=0)
            {
                strWhere.Append(" and t.BusinessType ='");
                strWhere.Append(imgCombox_BusinessType.Properties.Items[
                    imgCombox_BusinessType.SelectedIndex].Description);
                strWhere.Append("' ");
            }
            if (imgeCombo_xzq.SelectedIndex!=0)
            {
                strWhere.Append(" and t.AdministratiVeregion ='");
                strWhere.Append(imgeCombo_xzq.Properties.Items[
                    imgeCombo_xzq.SelectedIndex].Description);
                strWhere.Append("' ");
            }
            if (imgeCombo_BusinessCircles.SelectedIndex != 0)
            {
                strWhere.Append(" and t.BusinessCircles ='");
                strWhere.Append(imgeCombo_BusinessCircles.Properties.Items[
                    imgeCombo_BusinessCircles.SelectedIndex].Description);
                strWhere.Append("' ");
            } 
            if (textEdit_Decorate.Text.Trim()!="")
            {
                strWhere.Append(" and  Decorate like '%");
                strWhere.Append(textEdit_Decorate.Text);
                strWhere.Append("%' ");
            }
            if (textEdit_priceEnd.Text!=""&&
                textEdit_priceEnd.Text != "0")
            {
                strWhere.Append(" and Price Between ");
                strWhere.Append(textEdit_priceBegin.Text);
                strWhere.Append(" and ");
                strWhere.Append(textEdit_priceEnd.Text);
            } 
            if (textEdit_bulidingAreaE.Text!=""&&
                textEdit_bulidingAreaE.Text!="0")
            {
                strWhere.Append(" and BuildingArea Between ");
                strWhere.Append(textEdit_bulidingAreaB.Text);
                strWhere.Append(" and ");
                strWhere.Append(textEdit_bulidingAreaE.Text);
            }
            if (textEdit_openningAreaE.Text!="" &&
                textEdit_openningAreaE.Text!="0")
            {
                //暂时不知从哪里获取
            }

            try
            {
                SqlClass sqlClass = new SqlClass();
                DataSet ds = sqlClass.BProjectGetList(strWhere.ToString());
                if (ds == null || ds.Tables.Count == 0)
                {
                    XtraMessageBox.Show("数据库查询出错！请检查数据库连接是否正常！");
                    return;
                }
               
                BeginInvoke(new MethodInvoker(() =>
                {
                    gridControl2.DataSource = ds.Tables[0];
                    
                    gridView2.GroupPanelText = "共记"
                        + ds.Tables[0].Rows.Count + "条数据";
                    btn_select.Enabled = true;
                }
                ));
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            }
        }

        

        private void btn_select_Click(object sender, EventArgs e)
        {
            if (!CheckDateEdit())
            {
                return;
            }
            BeginInvoke(new MethodInvoker(() =>
                {
                    gridView2.GroupPanelText = "正在查询...";
                    btn_select.Enabled = false;
                }));
            
            Thread t = new Thread(LoatData) { IsBackground = true };
            t.Start();
        }

   
        private bool CheckDateEdit()
        {
            if (DateTime.Parse(textEdit_OpenTimeEnd.Text)<
                DateTime.Parse(textEdit_OpenTimeBegin.Text))
            {
                XtraMessageBox.Show("搜索条件有误：开盘时间 范围中第二个值应大于第一个值！");
                return false;
            }
            if (textEdit_priceEnd.Text!="" && textEdit_priceBegin.Text!=""
                && int.Parse(textEdit_priceEnd.Text) 
                < int.Parse(textEdit_priceBegin.Text))
            {
                XtraMessageBox.Show("搜索条件有误：租金/售价 范围中第二个值应大于第一个值！");
                return false;
            }
            if (textEdit_bulidingAreaE.Text != "" 
                && textEdit_bulidingAreaB.Text != ""
                && int.Parse(textEdit_bulidingAreaE.Text)
                < int.Parse(textEdit_bulidingAreaB.Text))
            {
                XtraMessageBox.Show("搜索条件有误：建筑面积 范围中第二个值应大于第一个值！");
                return false;
            }
            if (textEdit_openningAreaE.Text != "" && 
                textEdit_openningAreaB.Text != ""
            && int.Parse(textEdit_openningAreaE.Text)
            < int.Parse(textEdit_openningAreaB.Text))
            {
                XtraMessageBox.Show("搜索条件有误：开盘面积 范围中第二个值应大于第一个值！");
                return false;
            } 
            return true;
        }

 

        private void btn_importResultExcel_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                return;
            }
            string fileName = "商业楼数据列表";
            saveFileDialog1.FileName = fileName;
            saveFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                gridView2.ExportToXls(saveFileDialog1.FileName);
                XtraMessageBox.Show("Excel导出完成！");
            }
        }

        private void textEdit_priceEnd_Leave(object sender, EventArgs e)
        {
            if (textEdit_priceEnd.Text=="0")
            {
                textEdit_priceEnd.Text = "";
            }
        }

        private void textEdit_bulidingAreaE_Leave(object sender, EventArgs e)
        {
            if (textEdit_bulidingAreaE.Text=="0")
            {
                textEdit_bulidingAreaE.Text = "";
            }
        }

        private void textEdit_openningAreaE_Leave(object sender, EventArgs e)
        {
            if (textEdit_openningAreaE.Text=="0")
            {
                textEdit_openningAreaE.Text = "";
            }
        }

        private void gridView2_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Button == MouseButtons.Left || _isDeletingProject)
            {
                return;
            }
            DataRow row = gridView2.GetDataRow(gridView2.FocusedRowHandle);
            if (row==null)
            {
                return;
            }
             
            popupMenu_Right.ShowPopup(Cursor.Position);    
                         
        }

        private void barBtnItem_refresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            btn_select_Click(null,null);
        }

        private void barBtnItem_Detail_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataRow row = gridView2.GetDataRow(gridView2.FocusedRowHandle);
            if (row == null)
            {
                return;
            }
            BProjectDetailForm form = new BProjectDetailForm(row["ProjectName"].ToString());
            form.MdiParent = this.MdiParent;
            form.Show();

        }

        private void barBtnItem_UpdateFile_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataRow row = gridView2.GetDataRow(gridView2.FocusedRowHandle);
            if (row == null)
            {
                return;
            }
            BUpdateFileForm form = new BUpdateFileForm(row["ProjectName"].ToString());
            form.ShowDialog();
        }

        private void barBtnItem_UpdateImages_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            DataRow row = gridView2.GetDataRow(gridView2.FocusedRowHandle);
            if (row == null)
            {
                return;
            }
            BUpdateImageForm form = new BUpdateImageForm(row["ProjectName"].ToString());
            form.ShowDialog();
        }

        private void barBtnItem_DeleteProject_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataRow row = gridView2.GetDataRow(gridView2.FocusedRowHandle);
            if (row == null)
            {
                return;
            }
            string proName = row["ProjectName"].ToString();             
            if (XtraMessageBox.Show("将删除该项目所有数据，是否确定删除项目【"
                +proName+"】?","警告：",MessageBoxButtons.YesNo)==DialogResult.No)
            {
                return;
            }
            btn_select.Enabled = false;
            Thread t = new Thread(DeleteProject) { IsBackground = true };
            t.Start(proName);

        }

        private void DeleteProject(object objProName)
        {
            string proName = objProName as string;
            int succeed = 0;
            string failedDt = "";

            string addSql = "delete from tbbasicinfo where ProjectName='"+
                proName+"'";
            int result = DbHelper.ExecuteSql(addSql);
            succeed += result;
            if (result==0)
            {
                failedDt += "【基本资料】 ";
            }
            
            addSql = "delete from tbbusinessmanage where ProjectName='" +
                proName + "'";
            result = DbHelper.ExecuteSql(addSql);
            succeed += result;
            if (result==0)
            {
                failedDt += "【经营管理】 ";
            }
            
            addSql = "delete from tbimagefile where ProjectName='" +
                proName + "'";
            result = DbHelper.ExecuteSql(addSql);
            succeed += result;
            if (result==0)
            {
                failedDt += "【项目图片】 ";
            }

            addSql = "delete from tbplanningdesign where ProjectName='" +
                proName + "'";
            result = DbHelper.ExecuteSql(addSql);
            succeed += result;
            if (result==0)
            {
                failedDt += "【规划设计】 ";
            }

            addSql = "delete from tbsummarize where ProjectName='" +
               proName + "'";
            result = DbHelper.ExecuteSql(addSql);
            succeed += result;
            if (result==0)
            {
                failedDt += "【项目总结】 ";
            }
            btn_select_Click(null,null);
            if (succeed>0)
            {
                XtraMessageBox.Show("项目删除成功！");
            }
        }
     
       
    }
}